/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace myBoard.App.Controls
{
    /// <summary>
    /// Interaction logic for LocalMagnetsExplorerItem.xaml
    /// </summary>
    /// 
    public enum MagnetExplorerStates
    {
        Logo,Thumb
    }

    public partial class LocalMagnetsExplorerItem : System.Windows.Controls.UserControl
    {
        private Point _draggingFadingZoneResetPosition;
        private Point _itemCoordinates;
        private Point _firstClickCoordinates;
        private myBoard.Framework.MagnetEx _magnet;
        MagnetExplorerStates _currentState;

        private Storyboard _morphToThumbSB;
        private Storyboard _morphToLogoSB;
        private Storyboard _toLogoSB;


        public myBoard.Framework.MagnetEx Magnet
        {
            get { return _magnet; }
            set { _magnet = value; }
        }

        public Point FirstClickCoordinates
        {
            get { return _firstClickCoordinates; }            
        }

        public Point DraggingFadingZoneResetPosition
        {
            get { return _draggingFadingZoneResetPosition; }
        }

        public LocalMagnetsExplorerItem()
        {
            this.InitializeComponent();
            base.MouseLeftButtonDown += new MouseButtonEventHandler(this.LocalMagnetsExplorerItem_MouseLeftButtonDown);
            base.Loaded += new RoutedEventHandler(this.LocalMagnetsExplorerItem_Loaded);
            _morphToThumbSB = this.Resources["MorphToThumb"] as Storyboard;
            _morphToLogoSB = this.Resources["MorphToLogo"] as Storyboard;
            _toLogoSB = this.Resources["ToLogo"] as Storyboard;
            _currentState = MagnetExplorerStates.Logo;
        }

        void LocalMagnetsExplorerItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _firstClickCoordinates = Mouse.GetPosition(this);
        }

        void LocalMagnetsExplorerItem_Loaded(object sender, RoutedEventArgs e)
        {
            CreateStoryboards();
            double draggingZoneXMiddle = DraggingFadingZone.ActualWidth / 2;
            double draggingZoneYMiddle = DraggingFadingZone.ActualHeight / 2;
            double logoZoneXMiddle = _logoZone.ActualWidth / 2;
            double logoZoneYMiddle = _logoZone.ActualHeight / 2;
            double xDiff = draggingZoneXMiddle - logoZoneXMiddle;
            double yDiff = draggingZoneYMiddle - logoZoneYMiddle;
            Canvas.SetLeft(DraggingFadingZone, -xDiff);
            Canvas.SetTop(DraggingFadingZone, -yDiff-14);
            _draggingFadingZoneResetPosition = new Point(-xDiff, -yDiff - 14);
            int i = 0;
            foreach (FrameworkElement element in ((StackPanel)this.Parent).Children)
            {
                if (element == this)
                {
                    _itemCoordinates = new Point(i * (element.Width + (element.Margin.Left + element.Margin.Right)), 0);
                }
                i++;
            }
        }

        public void ResetDraggingFadingZone()
        {
            Debug.WriteLine("ResetDraggingFadingZone");
            Canvas.SetLeft(DraggingFadingZone, _draggingFadingZoneResetPosition.X);
            Canvas.SetTop(DraggingFadingZone, _draggingFadingZoneResetPosition.Y);
            this.BeginStoryboard(_toLogoSB);
        }
 
        private void CreateStoryboards()
        {
            MorphToThumbStoryboardCreation();
        }

        private void MorphToThumbStoryboardCreation()
        {
            double xScaleFactor = _thumbMorphGrid.ActualWidth / _logoMorphGrid.ActualWidth;
            double yScaleFactor = _thumbMorphGrid.ActualHeight / _logoMorphGrid.ActualHeight;

            foreach (Timeline timeline in _morphToThumbSB.Children)
            {
                if (timeline.Name == "_logoMorphGridXScale")
                {
                    ((DoubleAnimationUsingKeyFrames)timeline).KeyFrames[0].Value = xScaleFactor;                                        
                }
                if (timeline.Name == "_logoMorphGridYScale")
                {
                    ((DoubleAnimationUsingKeyFrames)timeline).KeyFrames[0].Value = yScaleFactor;
                } 
            }
        }

        public void MorphToThumb()
        {
            if (_currentState == MagnetExplorerStates.Logo)
            {
                _currentState = MagnetExplorerStates.Thumb;
                this.BeginStoryboard(_morphToThumbSB);
                
            }
        }

        public void MorphToLogo()
        {
            if (_currentState == MagnetExplorerStates.Thumb)
            {
                _currentState = MagnetExplorerStates.Logo;
                this.BeginStoryboard(_morphToLogoSB);
            }
        }

    }
}